<html>
<head>
<title>CpcPaChi - Version 0.02</title>
<style type="text/css">
  span
  {
    font-family: Monospace, monospace, 'Courier New';
    font-size: 10pt;
    color: #000000;
  }

  .style_1
  {
    color: #d00000;
    background-color: #ffffff;
  }
  .style_4
  {
    color: #007f00;
    background-color: #ffffff;
  }
  .style_5
  {
    color: #00007f;
    background-color: #ffffff;
    font-weight: bold;
  }
  .style_6
  {
    color: #ff901e;
    background-color: #ffffff;
  }
  .style_10
  {
    color: #301010;
    background-color: #ffffff;
  }
  .style_11
  {
    color: #000000;
    background-color: #ffffff;
  }
</style>
</head>
<body>
<h2>Un compilador sencillo paso a paso (2)</h2>

<p align="right">(Volver al <a href="doc.html">&iacute;ndice</a>) </p>

<p>Este segundo acercamiento va a reconocer dos &oacute;rdenes m&aacute;s: WRITECHAR, que escribir&aacute; una &uacute;nica letra en pantalla, y LOCATE, que mover&aacute; el cursor a otras coordenadas de pantalla. Adem&aacute;s, en vez de aceptar una &uacute;nica orden por teclado, leer&aacute; varias desde disco.</p>

<p>As&iacute;, aceptar&aacute; programas como &eacute;ste:</p>
<pre>
cls
locate(10,5)
writeChar('a')
locate(1,10)
writeChar('b')
</pre>


<p>El hecho de que el usuario indique el nombre del fichero no es mucha complicaci&oacute;n: vemos los argumentos de &quot;Main&quot;, y avisamos si no existen:</p>
<pre>
if (opciones.Length == 0)
{
  Console.WriteLine(
    "No se ha indicado el nombre del fichero");
  Environment.Exit(1);
}

nombreOrigen = opciones[0];
ficheroOrigen = File.OpenText(nombreOrigen);
ficheroDestino = File.CreateText("salida.bas");
</pre>


<p>En cuanto a la traducci&oacute;n de writeChar('a'), se convertir&iacute;a en dos &oacute;rdenes de ensamblador: LD A, &amp;61; CALL &amp;BB5A. El primer problema es que tenemos que crear un c&oacute;digo que no ser&aacute; siempre el mismo, sino que depender&aacute; del par&aacute;metro que indiquemos entre comillas. Nuestra aproximaci&oacute;n por ahora ser&aacute; "poco pr&aacute;ctica":tomar la letra que aparece en la posici&oacute;n 12 (la 11, contando desde cero) y usar su c&oacute;digo ASCII en hexadecimal:</p>

<pre>
char letra = orden[11];
byte caracter = Convert.ToByte(letra);
ficheroDestino.WriteLine(lineaActual
  + " DATA 3E," + Convert.ToString(caracter,16).ToUpper()
  + ": ' LD A, \"" + letra +"\"");
lineaActual += 10;
ficheroDestino.WriteLine(lineaActual+
  " DATA CD,5A,BB: ' CALL &BB5A - WRITECHAR" );
</pre>

<p>Adem&aacute;s, para cada nueva l&iacute;nea que generemos, aumentaremos el n&uacute;mero de l&iacute;nea del cargador en Basic y la cantidad de bytes de datos que hemos exportado:</p>
<pre>
lineaActual += 10;
longitudTotal += 5;
</pre>

<p>Claramente, esta forma de saber la letra no es buena. &iquest;Qu&eacute; ocurre si hay un espacio antes de las comillas? La letra que debemos escribir ya no estar&iacute;a en la posici&oacute;n 12. Lo mismo ocurrir&aacute; si hay alg&uacute;n espacio antes de WRITECHAR o del par&eacute;ntesis. Por eso, en la pr&oacute;xima aproximaci&oacute;n crearemos un primer analizador l&eacute;xico, que sea capaz de extraer correctamente una palabra, aunque tenga espacios delante.</p>

<p>Por lo que respecta a la traducci&oacute;n de LOCATE, tenemos otro problema: la posici&oacute;n puede ser un n&uacute;mero de una cifra o de dos. Hasta que creemos un analizador l&eacute;xico que nos ayude, de momento volveremos a hacerlo de forma artesanal:</p>
<ul>
  <li> Una alternativa ser&iacute;a leer una primera cifra, mirar si la siguiente es la coma o una cifra num&eacute;rica, en caso de que sea num&eacute;rica deber&iacute;amos multiplicar por 10 la cifra anterior y sum&aacute;rsela... muy trabajoso, pero es una soluci&oacute;n si usamos lenguajes como Pascal para crear nuestro compilador.</li>
  <li>Si usamos C#, hay una alternativa m&aacute;s elegante: preparar la subcadena que contiene los dos n&uacute;meros separados por la coma, usando &quot;Substring&quot; y partirla usando &quot;Split&quot; para obtener los dos n&uacute;meros:</li>
</ul>

<pre>
/* Descompongo, separando por la coma */
int ultimaPosicion = orden.IndexOf(")") - 1;
string bloqueCifras = orden.Substring( 7, ultimaPosicion-6 );
char [] delimitadores = {','};
string [] cifras = bloqueCifras.Split(delimitadores);
</pre>
<p>En un CPC hay dos formas de situar el cursor: LD A, columna; CALL &amp;BB6F y luego LD A, fila; CALL &amp;BB72, o bien indicar a la vez la fila y la columna el registro HL (H=columna, L=fila) y hacer CALL &amp;BB75. De momento usaremos la primera, que ser&aacute; algo m&aacute;s legible, aunque ocupe un poco m&aacute;s de espacio.</p>

<p>Con estas consideraciones, la parte que genera el c&oacute;digo para la primera parte de LOCATE, la columna, todav&iacute;a de forma muy rudimentaria y trabajosa, ser&aacute;:</p>

<pre>
/* La primera cifra (1 o 2 letras) es X */
int x = Convert.ToInt32( cifras[0] );
ficheroDestino.WriteLine(lineaActual
  + " DATA 3E," + Convert.ToString(x,16).ToUpper()
  + ": ' LD A, " + x );
lineaActual += 10;
ficheroDestino.WriteLine(lineaActual+
  " DATA CD,6F,BB: ' CALL &BB6F - CURSOR COLUMN" );
lineaActual += 10;
</pre>

<p><br />As&iacute;, el c&oacute;digo generado para el programita anterior ser&iacute;a:</p>

<pre>
10 DATA CD,6C,BB: ' CALL &amp;BB6C - CLS
20 DATA 3E,A: ' LD A, 10
30 DATA CD,6F,BB: ' CALL &amp;BB6F - CURSOR COLUMN
40 DATA 3E,5: ' LD A, 5
50 DATA CD,72,BB: ' CALL &amp;BB72 - CURSOR ROW
60 DATA 3E,61: ' LD A, "a"
70 DATA CD,5A,BB: ' CALL &amp;BB5A - WRITECHAR
80 DATA 3E,1: ' LD A, 1
90 DATA CD,6F,BB: ' CALL &amp;BB6F - CURSOR COLUMN
100 DATA 3E,A: ' LD A, 10
110 DATA CD,72,BB: ' CALL &amp;BB72 - CURSOR ROW
120 DATA 3E,62: ' LD A, "b"
130 DATA CD,5A,BB: ' CALL &amp;BB5A - WRITECHAR
140 DATA C9: ' RET
150 longitud = 33
160 MEMORY 39999
170 FOR n=40000 TO 40000+longitud
180 READ a$:POKE n,VAL("&amp;"+a$)
190 NEXT
200 CALL 40000
</pre>


<p><br /><br />Y el conjunto de todo el fuente del compilador quedar&iacute;a as&iacute;:</p>

<p>
<span class="style_1">/*&nbsp;cpcPaChi;</span><br />
<br />
&nbsp;&nbsp;&nbsp;<span class="style_1">Un&nbsp;compilador&nbsp;de&nbsp;Pascal&nbsp;chiquitito&nbsp;para&nbsp;CPC</span><br />
&nbsp;&nbsp;&nbsp;<span class="style_1">Por&nbsp;Nacho&nbsp;Cabanes&nbsp;-&nbsp;Version&nbsp;0.01cs,&nbsp;preliminar</span><br />
&nbsp;&nbsp;&nbsp;<span class="style_1">(en&nbsp;C#)</span><br />
<br />
&nbsp;&nbsp;&nbsp;<span class="style_1">Versiones&nbsp;hasta&nbsp;la&nbsp;fecha:</span><br />
<br />
&nbsp;&nbsp;&nbsp;<span class="style_1">Num.&nbsp;&nbsp;&nbsp;Fecha&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cambios</span><br />
&nbsp;&nbsp;&nbsp;<span class="style_1">---------------------------------------------------</span><br />
&nbsp;&nbsp;&nbsp;<span class="style_1">0.02cs&nbsp;&nbsp;18-Oct-2010&nbsp;&nbsp;Admite&nbsp;CLS,&nbsp;LOCATE,&nbsp;WRITECHAR,</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_1">que&nbsp;lee&nbsp;desde&nbsp;fichero.&nbsp;Solo&nbsp;permite</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_1">una&nbsp;orden&nbsp;en&nbsp;cada&nbsp;linea,&nbsp;con&nbsp;formato&nbsp;fijo</span><br />
<br />
&nbsp;&nbsp;&nbsp;<span class="style_1">0.01cs&nbsp;&nbsp;15-Oct-2010&nbsp;&nbsp;Preliminar:&nbsp;solo&nbsp;acepta&nbsp;CLS&nbsp;por&nbsp;teclado</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_1">y&nbsp;genera&nbsp;el&nbsp;codigo&nbsp;correspondiente</span><br />
<span class="style_1">*/</span><br />
<br />
<span class="style_5">using&nbsp;</span><span class="style_11">System</span><span class="style_10">;</span><br />
<span class="style_5">using&nbsp;</span><span class="style_11">System</span><span class="style_10">.</span><span class="style_11">IO</span><span class="style_10">;</span><br />
<br />
<span class="style_5">class&nbsp;</span><span class="style_19">cpcpachi</span><br />
<span class="style_10">{</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;string&nbsp;</span><span class="style_11">orden</span><span class="style_10">;</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;string&nbsp;</span><span class="style_11">nombreOrigen</span><span class="style_10">;</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;</span><span class="style_11">StreamReader&nbsp;ficheroOrigen</span><span class="style_10">;</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;</span><span class="style_11">StreamWriter&nbsp;ficheroDestino</span><span class="style_10">;</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;int&nbsp;</span><span class="style_11">lineaActual&nbsp;</span><span class="style_10">=&nbsp;</span><span class="style_4">10</span><span class="style_10">;</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;int&nbsp;</span><span class="style_11">longitudTotal&nbsp;</span><span class="style_10">=&nbsp;</span><span class="style_4">0</span><span class="style_10">;</span><br />
<br />
<br />
&nbsp;&nbsp;<span class="style_1">/*&nbsp;Analiza&nbsp;las&nbsp;opciones&nbsp;que&nbsp;teclee&nbsp;el&nbsp;usuario.&nbsp;Por&nbsp;ahora,</span><br />
&nbsp;&nbsp;<span class="style_1">solo&nbsp;el&nbsp;nombre&nbsp;del&nbsp;fichero&nbsp;a&nbsp;compilar&nbsp;*/</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;void&nbsp;</span><span class="style_11">analizarOpciones</span><span class="style_10">(</span><span class="style_5">string</span><span class="style_10">[]&nbsp;</span><span class="style_11">opciones</span><span class="style_10">)</span><br />
&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_5">if&nbsp;</span><span class="style_10">(</span><span class="style_11">opciones</span><span class="style_10">.</span><span class="style_11">Length&nbsp;</span><span class="style_10">==&nbsp;</span><span class="style_4">0</span><span class="style_10">)</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">Console</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_6">"No&nbsp;se&nbsp;ha&nbsp;indicado&nbsp;el&nbsp;nombre&nbsp;del&nbsp;fichero"</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">Environment</span><span class="style_10">.</span><span class="style_11">Exit</span><span class="style_10">(</span><span class="style_4">1</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">}</span><br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">nombreOrigen&nbsp;</span><span class="style_10">=&nbsp;</span><span class="style_11">opciones</span><span class="style_10">[</span><span class="style_4">0</span><span class="style_10">];</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroOrigen&nbsp;</span><span class="style_10">=&nbsp;</span><span class="style_11">File</span><span class="style_10">.</span><span class="style_11">OpenText</span><span class="style_10">(</span><span class="style_11">nombreOrigen</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino&nbsp;</span><span class="style_10">=&nbsp;</span><span class="style_11">File</span><span class="style_10">.</span><span class="style_11">CreateText</span><span class="style_10">(</span><span class="style_6">"salida.bas"</span><span class="style_10">);</span><br />
&nbsp;&nbsp;<span class="style_10">}</span><br />
<br />
&nbsp;&nbsp;<span class="style_1">/*&nbsp;Obtiene&nbsp;una&nbsp;orden&nbsp;del&nbsp;programa&nbsp;fuente.&nbsp;La&nbsp;toma&nbsp;del&nbsp;fichero&nbsp;de&nbsp;origen&nbsp;*/</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;void&nbsp;</span><span class="style_11">obtenerOrden</span><span class="style_10">()</span><br />
&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">orden&nbsp;</span><span class="style_10">=&nbsp;</span><span class="style_11">ficheroOrigen</span><span class="style_10">.</span><span class="style_11">ReadLine</span><span class="style_10">();</span><br />
&nbsp;&nbsp;<span class="style_10">}</span><br />
<br />
&nbsp;&nbsp;<span class="style_1">/*&nbsp;Analiza&nbsp;la&nbsp;orden&nbsp;que&nbsp;el&nbsp;usuario&nbsp;ha&nbsp;dado,&nbsp;y&nbsp;sale&nbsp;con&nbsp;un&nbsp;mensaje</span><br />
&nbsp;&nbsp;<span class="style_1">de&nbsp;error&nbsp;si&nbsp;es&nbsp;incorrecta&nbsp;*/</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;void&nbsp;</span><span class="style_11">analizarUnaOrden</span><span class="style_10">()</span><br />
&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_5">if&nbsp;</span><span class="style_10">((&nbsp;</span><span class="style_11">orden</span><span class="style_10">.</span><span class="style_11">ToUpper</span><span class="style_10">()&nbsp;!=&nbsp;</span><span class="style_6">"CLS"&nbsp;</span><span class="style_10">)</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">&amp;&amp;&nbsp;(</span><span class="style_11">orden</span><span class="style_10">.</span><span class="style_11">ToUpper</span><span class="style_10">().</span><span class="style_11">IndexOf</span><span class="style_10">(</span><span class="style_6">"LOCATE"</span><span class="style_10">)&nbsp;!=</span><span class="style_4">0&nbsp;</span><span class="style_10">)</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">&amp;&amp;&nbsp;(</span><span class="style_11">orden</span><span class="style_10">.</span><span class="style_11">ToUpper</span><span class="style_10">().</span><span class="style_11">IndexOf</span><span class="style_10">(</span><span class="style_6">"WRITECHAR"</span><span class="style_10">)&nbsp;!=</span><span class="style_4">0&nbsp;</span><span class="style_10">)&nbsp;)</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">Console</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_6">"Error:&nbsp;orden&nbsp;{0}&nbsp;no&nbsp;reconocida.&nbsp;Se&nbsp;esperaba:&nbsp;CLS,&nbsp;LOCATE&nbsp;o&nbsp;WRITECHAR"</span><span class="style_10">,</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">orden</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">Environment</span><span class="style_10">.</span><span class="style_11">Exit</span><span class="style_10">(</span><span class="style_4">1</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">}</span><br />
&nbsp;&nbsp;<span class="style_10">}</span><br />
<br />
&nbsp;&nbsp;<span class="style_1">/*&nbsp;Genera&nbsp;el&nbsp;codigo&nbsp;de&nbsp;destino:&nbsp;codigo&nbsp;maquina&nbsp;de&nbsp;Amstrad&nbsp;CPC</span><br />
&nbsp;&nbsp;<span class="style_1">dentro&nbsp;de&nbsp;un&nbsp;cargador&nbsp;en&nbsp;Basic&nbsp;*/</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;void&nbsp;</span><span class="style_11">generarCodigoUnaOrden</span><span class="style_10">()</span><br />
&nbsp;&nbsp;<span class="style_10">{</span><br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_5">if&nbsp;</span><span class="style_10">(&nbsp;</span><span class="style_11">orden</span><span class="style_10">.</span><span class="style_11">ToUpper</span><span class="style_10">()&nbsp;&nbsp;==&nbsp;</span><span class="style_6">"CLS"</span><span class="style_10">)</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_11">lineaActual</span><span class="style_10">+</span><span class="style_6">"&nbsp;DATA&nbsp;CD,6C,BB:&nbsp;'&nbsp;CALL&nbsp;&amp;BB6C&nbsp;-&nbsp;CLS"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">lineaActual&nbsp;</span><span class="style_10">+=&nbsp;</span><span class="style_4">10</span><span class="style_10">;</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">longitudTotal&nbsp;</span><span class="style_10">+=&nbsp;</span><span class="style_4">3</span><span class="style_10">;</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">}</span><br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_5">if&nbsp;</span><span class="style_10">(&nbsp;</span><span class="style_11">orden</span><span class="style_10">.</span><span class="style_11">ToUpper</span><span class="style_10">().</span><span class="style_11">IndexOf</span><span class="style_10">(</span><span class="style_6">"WRITECHAR"</span><span class="style_10">)&nbsp;==&nbsp;</span><span class="style_4">0&nbsp;</span><span class="style_10">)</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_5">char&nbsp;</span><span class="style_11">letra&nbsp;</span><span class="style_10">=&nbsp;</span><span class="style_11">orden</span><span class="style_10">[</span><span class="style_4">11</span><span class="style_10">];</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_5">byte&nbsp;</span><span class="style_11">caracter&nbsp;</span><span class="style_10">=&nbsp;</span><span class="style_11">Convert</span><span class="style_10">.</span><span class="style_11">ToByte</span><span class="style_10">(</span><span class="style_11">letra</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_11">lineaActual</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">+&nbsp;</span><span class="style_6">"&nbsp;DATA&nbsp;3E,"&nbsp;</span><span class="style_10">+&nbsp;</span><span class="style_11">Convert</span><span class="style_10">.</span><span class="style_11">ToString</span><span class="style_10">(</span><span class="style_11">caracter</span><span class="style_10">,</span><span class="style_4">16</span><span class="style_10">).</span><span class="style_11">ToUpper</span><span class="style_10">()</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">+&nbsp;</span><span class="style_6">":&nbsp;'&nbsp;LD&nbsp;A,&nbsp;\""&nbsp;</span><span class="style_10">+&nbsp;</span><span class="style_11">letra&nbsp;</span><span class="style_10">+</span><span class="style_6">"\""</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">lineaActual&nbsp;</span><span class="style_10">+=&nbsp;</span><span class="style_4">10</span><span class="style_10">;</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_11">lineaActual</span><span class="style_10">+</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_6">"&nbsp;DATA&nbsp;CD,5A,BB:&nbsp;'&nbsp;CALL&nbsp;&amp;BB5A&nbsp;-&nbsp;WRITECHAR"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">lineaActual&nbsp;</span><span class="style_10">+=&nbsp;</span><span class="style_4">10</span><span class="style_10">;</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">longitudTotal&nbsp;</span><span class="style_10">+=&nbsp;</span><span class="style_4">5</span><span class="style_10">;</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">}</span><br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_5">if&nbsp;</span><span class="style_10">(&nbsp;</span><span class="style_11">orden</span><span class="style_10">.</span><span class="style_11">ToUpper</span><span class="style_10">().</span><span class="style_11">IndexOf</span><span class="style_10">(</span><span class="style_6">"LOCATE"</span><span class="style_10">)&nbsp;==&nbsp;</span><span class="style_4">0&nbsp;</span><span class="style_10">)</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_1">/*&nbsp;Descompongo,&nbsp;separando&nbsp;por&nbsp;la&nbsp;coma&nbsp;*/</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_5">int&nbsp;</span><span class="style_11">ultimaPosicion&nbsp;</span><span class="style_10">=&nbsp;</span><span class="style_11">orden</span><span class="style_10">.</span><span class="style_11">IndexOf</span><span class="style_10">(</span><span class="style_6">")"</span><span class="style_10">)&nbsp;-&nbsp;</span><span class="style_4">1</span><span class="style_10">;</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_5">string&nbsp;</span><span class="style_11">bloqueCifras&nbsp;</span><span class="style_10">=&nbsp;</span><span class="style_11">orden</span><span class="style_10">.</span><span class="style_11">Substring</span><span class="style_10">(&nbsp;</span><span class="style_4">7</span><span class="style_10">,&nbsp;</span><span class="style_11">ultimaPosicion</span><span class="style_10">-</span><span class="style_4">6&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_5">char&nbsp;</span><span class="style_10">[]&nbsp;</span><span class="style_11">delimitadores&nbsp;</span><span class="style_10">=&nbsp;{</span><span class="style_7">','</span><span class="style_10">};</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_5">string&nbsp;</span><span class="style_10">[]&nbsp;</span><span class="style_11">cifras&nbsp;</span><span class="style_10">=&nbsp;</span><span class="style_11">bloqueCifras</span><span class="style_10">.</span><span class="style_11">Split</span><span class="style_10">(</span><span class="style_11">delimitadores</span><span class="style_10">);</span><br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_1">/*&nbsp;La&nbsp;primera&nbsp;cifra&nbsp;(1&nbsp;o&nbsp;2&nbsp;letras)&nbsp;es&nbsp;X&nbsp;*/</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_5">int&nbsp;</span><span class="style_11">x&nbsp;</span><span class="style_10">=&nbsp;</span><span class="style_11">Convert</span><span class="style_10">.</span><span class="style_11">ToInt32</span><span class="style_10">(&nbsp;</span><span class="style_11">cifras</span><span class="style_10">[</span><span class="style_4">0</span><span class="style_10">]&nbsp;);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_11">lineaActual</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">+&nbsp;</span><span class="style_6">"&nbsp;DATA&nbsp;3E,"&nbsp;</span><span class="style_10">+&nbsp;</span><span class="style_11">Convert</span><span class="style_10">.</span><span class="style_11">ToString</span><span class="style_10">(</span><span class="style_11">x</span><span class="style_10">,</span><span class="style_4">16</span><span class="style_10">).</span><span class="style_11">ToUpper</span><span class="style_10">()</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">+&nbsp;</span><span class="style_6">":&nbsp;'&nbsp;LD&nbsp;A,&nbsp;"&nbsp;</span><span class="style_10">+&nbsp;</span><span class="style_11">x&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">lineaActual&nbsp;</span><span class="style_10">+=&nbsp;</span><span class="style_4">10</span><span class="style_10">;</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_11">lineaActual</span><span class="style_10">+</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_6">"&nbsp;DATA&nbsp;CD,6F,BB:&nbsp;'&nbsp;CALL&nbsp;&amp;BB6F&nbsp;-&nbsp;CURSOR&nbsp;COLUMN"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">lineaActual&nbsp;</span><span class="style_10">+=&nbsp;</span><span class="style_4">10</span><span class="style_10">;</span><br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_1">/*&nbsp;La&nbsp;segunda&nbsp;cifra&nbsp;es&nbsp;Y&nbsp;*/</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_5">int&nbsp;</span><span class="style_11">y&nbsp;</span><span class="style_10">=&nbsp;</span><span class="style_11">Convert</span><span class="style_10">.</span><span class="style_11">ToInt32</span><span class="style_10">(&nbsp;</span><span class="style_11">cifras</span><span class="style_10">[</span><span class="style_4">1</span><span class="style_10">]&nbsp;);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_11">lineaActual</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">+&nbsp;</span><span class="style_6">"&nbsp;DATA&nbsp;3E,"&nbsp;</span><span class="style_10">+&nbsp;</span><span class="style_11">Convert</span><span class="style_10">.</span><span class="style_11">ToString</span><span class="style_10">(</span><span class="style_11">y</span><span class="style_10">,</span><span class="style_4">16</span><span class="style_10">).</span><span class="style_11">ToUpper</span><span class="style_10">()</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">+&nbsp;</span><span class="style_6">":&nbsp;'&nbsp;LD&nbsp;A,&nbsp;"&nbsp;</span><span class="style_10">+&nbsp;</span><span class="style_11">y&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">lineaActual&nbsp;</span><span class="style_10">+=&nbsp;</span><span class="style_4">10</span><span class="style_10">;</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_11">lineaActual</span><span class="style_10">+</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_6">"&nbsp;DATA&nbsp;CD,72,BB:&nbsp;'&nbsp;CALL&nbsp;&amp;BB72&nbsp;-&nbsp;CURSOR&nbsp;ROW"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">lineaActual&nbsp;</span><span class="style_10">+=&nbsp;</span><span class="style_4">10</span><span class="style_10">;</span><br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">longitudTotal&nbsp;</span><span class="style_10">+=&nbsp;</span><span class="style_4">10</span><span class="style_10">;</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">}</span><br />
<br />
&nbsp;&nbsp;<span class="style_10">}</span><br />
<br />
&nbsp;&nbsp;<span class="style_1">/*&nbsp;Genera&nbsp;el&nbsp;codigo&nbsp;de&nbsp;destino&nbsp;final:&nbsp;cargador&nbsp;Basic&nbsp;*/</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;void&nbsp;</span><span class="style_11">generarCodigoFinal</span><span class="style_10">()</span><br />
&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_11">lineaActual</span><span class="style_10">+</span><span class="style_6">"&nbsp;DATA&nbsp;C9:&nbsp;'&nbsp;RET"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_11">lineaActual</span><span class="style_10">+</span><span class="style_4">10</span><span class="style_10">+</span><span class="style_6">"&nbsp;longitud&nbsp;=&nbsp;"</span><span class="style_10">+</span><span class="style_11">longitudTotal&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_11">lineaActual</span><span class="style_10">+</span><span class="style_4">20</span><span class="style_10">+</span><span class="style_6">"&nbsp;MEMORY&nbsp;39999"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_11">lineaActual</span><span class="style_10">+</span><span class="style_4">30</span><span class="style_10">+</span><span class="style_6">"&nbsp;FOR&nbsp;n=40000&nbsp;TO&nbsp;40000+longitud"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_11">lineaActual</span><span class="style_10">+</span><span class="style_4">40</span><span class="style_10">+</span><span class="style_6">"&nbsp;READ&nbsp;a$:POKE&nbsp;n,VAL(\"&amp;\"+a$)"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_11">lineaActual</span><span class="style_10">+</span><span class="style_4">50</span><span class="style_10">+</span><span class="style_6">"&nbsp;NEXT"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_11">lineaActual</span><span class="style_10">+</span><span class="style_4">60</span><span class="style_10">+</span><span class="style_6">"&nbsp;CALL&nbsp;40000"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">Close</span><span class="style_10">();</span><br />
&nbsp;&nbsp;<span class="style_10">}</span><br />
<br />
&nbsp;&nbsp;<span class="style_1">/*&nbsp;Cuerpo&nbsp;del&nbsp;programa&nbsp;*/</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;void&nbsp;</span><span class="style_11">Main</span><span class="style_10">(</span><span class="style_5">string</span><span class="style_10">[]&nbsp;</span><span class="style_11">opciones</span><span class="style_10">)</span><br />
&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">analizarOpciones</span><span class="style_10">(</span><span class="style_11">opciones</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">obtenerOrden</span><span class="style_10">();</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_5">while&nbsp;</span><span class="style_10">((</span><span class="style_11">orden&nbsp;</span><span class="style_10">!=&nbsp;</span><span class="style_5">null</span><span class="style_10">)&nbsp;&amp;&amp;&nbsp;(</span><span class="style_11">orden&nbsp;</span><span class="style_10">!=&nbsp;</span><span class="style_6">""</span><span class="style_10">))</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">analizarUnaOrden</span><span class="style_10">();</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">generarCodigoUnaOrden</span><span class="style_10">();</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">obtenerOrden</span><span class="style_10">();</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">}</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">generarCodigoFinal</span><span class="style_10">();</span><br />
&nbsp;&nbsp;<span class="style_10">}</span><br />
<span class="style_10">}</span><br />

</p>

<p align="right">(Volver al <a href="doc.html">&iacute;ndice</a>) </p>
</body>
</html>
